/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.analyzers;

import java.util.Iterator;
import java.util.List;

import org.jftstool.analyzers.exceptions.AnalyzerManagerException;
import org.jftstool.config.AnalyzerConfig;
import org.jftstool.config.JFTSConfiguration;
import org.jftstool.config.exceptions.JFTSConfigException;

/**
 * @author grupo JFTS
 *
 */
public class AnalyzerManager {
	
	private JFTSConfiguration config = null;
	
	public AnalyzerManager() throws AnalyzerManagerException{
		try {
			config = JFTSConfiguration.getInstance();
		}
		catch (JFTSConfigException e) {
			throw new AnalyzerManagerException(e.getMessage());
		}
	}
	
	
	/**
	 * Returns the analyzer for the language received as a parameter
	 * @param language specifies the language of the analyzer to return	 * @return
	 * @throws AnalyzerManagerException
	 */
	public JFTSAnalyzer getAnalyzer(String language) throws AnalyzerManagerException{
		String className = null;
		JFTSAnalyzer dinamicAnalyzer = null;
		
		if (_validateLanguage(language)){
			try {
				AnalyzerConfig analyzer = null;
				boolean find = false;
				
				List<AnalyzerConfig> analyzers = config.getAnalyzers();
				Iterator<AnalyzerConfig> it = analyzers.iterator();
				
				while (it.hasNext() && !find){
					AnalyzerConfig aux = it.next();
					if (aux.getLanguage().trim().toLowerCase().equals(language.toLowerCase())){
						analyzer = config.getAnalyzer(aux.getAnalyzerName());
						find = true;
					}
				}
				
				if (find == true){
					className = analyzer.getAnalyzerClass();
					Class<? extends JFTSAnalyzer> analyzerClass;
					analyzerClass = Class.forName(className).asSubclass(JFTSAnalyzer.class);
					dinamicAnalyzer = (JFTSAnalyzer) analyzerClass.newInstance();
				}
			}
			catch (JFTSConfigException e) {
				throw new AnalyzerManagerException(e.getMessage());
			}		
			catch (ClassNotFoundException e) {
				throw new AnalyzerManagerException("Error, cant't find class: " + className); 
			}
			catch (InstantiationException e) {
				throw new AnalyzerManagerException("Error to load class: " + className);					
			} 
			catch (IllegalAccessException e) {
				throw new AnalyzerManagerException("Error to load class: " + className);					
			}
		}
		return dinamicAnalyzer;
	}
	
	/**
	 * Returns an array containing the languages supported by JFTS
	 * @return String[]
	 */
	public String[] getSupportedLanguages(){
		String[] result = null;
		try {
			result = config.getSupportedLanguages();
		}
		catch (JFTSConfigException e) { /* do nothing*/ }
		return result;
	}
	
	public String getDefaultLanguage(){
		String[] languages = this.getSupportedLanguages();			
		String result = null;
		if (languages != null){
			result = languages[0];
		}
		return result;
	}
	
	
	private boolean _validateLanguage(String language){
		boolean exists = false;
		String[] languages = this.getSupportedLanguages();
		
		if (languages != null){
			int i = 0;
			while(i <= languages.length && !exists){
				if (languages[i].trim().equalsIgnoreCase(language.trim())){
					exists = true;
				}
				i++;
			}
		}
		return exists;
	}
}